package com.wiretap.action.admin.manage;

import java.io.PrintWriter;

import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.Contact;
import com.wiretap.service.ContactService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.Pager;

public class ContactManageAction extends WiretapActionSupport {

	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	private static final long serialVersionUID = 3098610836187798119L;
	private String phone;
	private int cPage;
	private ContactService contactService;
	private Pager<Contact> contacts;
	private String cName;
	private String cNumber;

	/**
	 * 查询所有通讯录列表
	 */
	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		if (!CommenUtil.isEmpty(phone)) {
			contacts = contactService.getAllContactByPhone(phone, cPage, Constants.DEFAULT_PAGESIEZE);
		}
		return SUCCESS;
	}

	public void addContact() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(cName) || CommenUtil.isEmpty(cNumber) || CommenUtil.isEmpty(phone)) {
			out.print("error");
		} else {
			contactService.addContact(cName, cNumber, phone);
			out.print("success");
		}
		out.flush();
		out.close();
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public ContactService getContactService() {
		return contactService;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	public Pager<Contact> getContacts() {
		return contacts;
	}

	public void setContacts(Pager<Contact> contacts) {
		this.contacts = contacts;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		this.cName = cName;
	}

	public String getcNumber() {
		return cNumber;
	}

	public void setcNumber(String cNumber) {
		this.cNumber = cNumber;
	}

}
